package com.springboot.cloud.demos.producer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.cloud.common.web.redis.CacheDuration;
import com.springboot.cloud.demos.entity.param.DictionaryQueryParam;
import com.springboot.cloud.demos.entity.po.Dictionary;
import com.springboot.cloud.demos.producer.dao.DictionaryMapper;
import com.springboot.cloud.demos.producer.service.IDictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
@RefreshScope
@CacheDuration(60*60*24)
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements IDictionaryService {

    @Value("${producer.dictionary:123}")
    private String value;

    @Override
    @CacheEvict(value = "dictionary",allEntries=true)
    public boolean add(Dictionary dictionary) {
        return this.save(dictionary);
    }

    @Override
    @CacheEvict(value = "dictionary", key = "#root.targetClass+'-'+#id")
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    @CacheEvict(value = "dictionary", key = "#root.targetClass+'-'+#dictionary.id")
    public boolean update(Dictionary dictionary) {
        return this.updateById(dictionary);
    }

    @Override
    @Cacheable(value = "dictionary", key = "#root.targetClass+'-'+#id")
    public Dictionary get(String id) {
        log.info("value:{}", value);
        return this.getById(id);
    }

    @Override
//    @Cacheable(value = "dictionary", key = "#root.targetClass+'-query-'+param.keyNo")
    public List<Dictionary> query(DictionaryQueryParam param) {
        log.info("query param :{}", param);
        QueryWrapper<Dictionary> queryWrapper = param.build();
        if (StringUtils.isNotEmpty(param.getKeyNo())) {
            queryWrapper.eq("key_no",param.getKeyNo());
        }
        return this.list(queryWrapper);
    }

    @Override
    @Cacheable(value = "dictionary", key = "#root.targetClass+'list'")
    public List<Dictionary> list() {
        QueryWrapper<Dictionary> queryWrapper =new QueryWrapper<>();
        return this.list(queryWrapper);
    }
}
